yjx.controller('profileController', ['$rootScope', '$scope', '$fetch', '$state', 'modalService', '$timeout', '$init', 'ipCookie', function ($rootScope, $scope, $fetch, $state, modalService, $timeout, $init, ipCookie) {
    var editApi = '/user/update/info';

    $init.title("基本信息");
    var params = {};
    $scope.edit = {};
    $scope.sexes = [
        { id: 1, value: '男' },
        { id: 2, value: '女' }
    ]
    $rootScope.userId = $rootScope.userId ? $rootScope.userId : ipCookie('userId')
    $scope.edit = $rootScope.user ? $rootScope.user : ipCookie('user');
    $scope.edit.sex = $rootScope.user.sex ? $rootScope.user.sex : $scope.sexes[0].value;
    /**选择性别 */
    $scope.selectSex = function (id, value) {
        $scope.edit.sex = value;
    }
    /**剩余字数 */
    $scope.left = function(){
        if($scope.edit.digests){
            return 50 - $scope.edit.digests.length;
        }else{
            return 50;
        }
    }
    /**表单验证 */
    $scope.editAct = function () {
        if (!$scope.edit.realname) {
            modalService.mainMsgError("请填写姓名");
            return;
        }
        if (isNaN($scope.edit.age)) {
            modalService.mainMsgError("年龄必须是数字")
        }

        if (!$scope.edit.telephone) {
            modalService.mainMsgError("请填写手机号")
            return;
        }
        if (!$scope.edit.telephone.match("^[1][34578][0-9]{9}$")) {
            modalService.mainMsgError("手机号格式不正确")
            return;
        }
        initparams();
    }
    /**处理参数 */
    function initparams() {
        params = {
            realname:$scope.edit.realname,
            id: $rootScope.userId,
            sex: $scope.edit.sex,
            telephone: $scope.edit.telephone
        }
        if ($scope.edit.age) {
            params.age = $scope.edit.age;
        }
        if ($scope.edit.email) {
            params.email = $scope.edit.email;
        }
        if ($scope.edit.company) {
            params.company = $scope.edit.company;
        }
        if ($scope.edit.job) {
            params.job = $scope.edit.job;
        }
        if ($scope.edit.address) {
            params.address = $scope.edit.addressDetail;
        }
        if ($scope.edit.digests) {
            params.digests = $scope.edit.digests;
        }
        submit();
    }
    /**保存修改 */
    function submit() {
        $fetch.fetch(
            editApi,
            {
                params: params,
                cache: false,
                type: 'post'
            }
        ).then(function (data) {
            $timeout(function () {
                $rootScope.user.realname = $scope.edit.realname;
                $rootScope.user.age = $scope.edit.age;
                $rootScope.user.sex = $scope.edit.sex;
                $rootScope.user.telephone = $scope.edit.telephone;
                $rootScope.user.email = $scope.edit.email;
                $rootScope.user.company = $scope.edit.company;
                $rootScope.user.job = $scope.edit.job;
                $rootScope.user.address = $scope.edit.address;
                $rootScope.user.digestss = $scope.edit.digestss;
                ipCookie('user',$rootScope.user,ipCookiesParam);
                history.back();
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }


}])